<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 5/31/24
 * Time: 2:51 PM
 */

namespace app\adminapi\lists\risk_report;

use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\logic\auth\AdminLogic;
use app\adminapi\logic\risk_report\ReportLogic;
use app\common\lists\ListsSearchInterface;
use app\common\model\auth\Admin;
use app\common\model\media_account\MediaAccount;
use app\common\model\promotion\PromotionLink;
use app\common\model\risk_report\RiskReport;


/**
 * 风控报表
 *
 * Class OrderLists
 * @package app\adminapi\lists
 */
class ReportLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * 搜索条件
     *
     * @return array
     */
    public function setSearch(): array
    {
        return [];
    }

    /**
     * 搜索条件
     *
     */
    public function queryWhere()
    {
        $params          = $this->request->get();
        $mediaId         = $params['media_id'] ?? '';
        $platformId      = $params['platform_id'] ?? '';
        $adminId         = $params['admin_id'] ?? '';
        $startDate       = $params['start_date'] ?? '';
        $endDate         = $params['end_date'] ?? '';
        $promotionLinkId = $params['promotion_link_id'] ?? '';
        $pAdminIds       = [];

        // TODO 管理员权限控制
        if ($this->adminInfo['root'] != 1) {
            $pAdminIds = AdminLogic::getPermissionAdminIds($this->adminInfo);
        }
        $promotion_where = [];
        if ($promotionLinkId) {
            $promotion_where[] = ['id', '=', $promotionLinkId];
        }
        if ($adminId) {
            $promotion_where[] = ['admin_id', '=', $adminId];
        }
        if ($pAdminIds) {
            $promotion_where[] = ['admin_id', 'in', $pAdminIds];
        }
        if ($mediaId) {
            $promotion_where[] = ['media', '=', $mediaId];
        }
        if ($platformId) {
            $promotion_where[] = ['platform', '=', $platformId];
        }
        $where              = [];
        $promotionLinkIDArr = PromotionLink::where($promotion_where)->column('id');
        if ($startDate && $endDate) {
            $where[] = ['count_date', 'between', [$startDate, $endDate]];
        }
        $where[] = ['promotion_link_id', 'in', $promotionLinkIDArr];

        return $where;
    }


    /**
     * 分页计数
     *
     * @return int
     */
    public function count(): int
    {
        return RiskReport::where($this->queryWhere())->group('count_date,promotion_link_id')->select()->count();
    }


    /**
     * 回收列表
     *
     * @return array
     */
    public function lists(): array
    {
        $list = RiskReport::field('
                count_date, promotion_link_id, advertiser_id,pitcher_id,
                sum(cost_money) as total_cost_money,
                sum(add_up_recharge_money) as total_add_up_recharge_money, 
                sum(add_desktop_num) as total_add_desktop_num, 
                sum(new_fans_pay_num) as total_new_fans_pay_num,
                sum(new_fans_recharge_money) as total_new_fans_recharge_money
            ')
            ->where($this->queryWhere())
            ->group('count_date,promotion_link_id')
            ->order('count_date', 'desc')
            ->limit($this->limitOffset, $this->limitLength)
            ->select();

        $vl = new ReportLogic();

        foreach ($list as &$item) {
            $item->add_up_recharge_money   = $item->total_add_up_recharge_money;
            $item->new_fans_recharge_money = $item->total_new_fans_recharge_money;
            $item->new_fans_pay_num        = $item->total_new_fans_pay_num;
            $item->add_desktop_num         = $item->total_add_desktop_num;

            $item->add_up_recycle_rate = getRate($item->add_up_recharge_money, $item->cost_money); // 累计回收率
            $item->roi                 = getRate($item->new_fans_recharge_money, $item->cost_money);  // 首日回收roi
            $item->add_desktop_cost    = getDiv($item->cost_money, $item->add_desktop_num);    // 加桌成本
            $item->new_fans_pay_price  = getDiv($item->cost_momey, $item->new_fans_pay_num); // 付费新粉单价
            // 素材
            $item->creative        = $vl->getCreative($item->promotion_link_id, $this->request->get('charge', 100));
            $item->pitcher_name    = Admin::where('id', $item->pitcher_id)->value('name') ?? '';
            $item->advertiser_name = MediaAccount::where('advertiser_id', $item->advertiser_id)->value('advertiser_name') ?? '';
        }

        return $list->toArray();
    }


}
